package com.hzlj.position.locate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.ObjectUtil;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.dict.annotation.DictConvert;
import com.hzlj.position.locate.common.dto.abnGathering.AbnGatheringCacheDTO;
import com.hzlj.position.locate.common.dto.abnGatheringJzry.AbnGatheringJzryQueryParamDTO;
import com.hzlj.position.locate.common.dto.abnGatheringJzry.AbnGatheringJzryQueryResultDTO;
import com.hzlj.position.locate.common.model.AbnGatheringJzry;
import com.hzlj.position.locate.dao.AbnGatheringJzryDao;
import com.hzlj.position.locate.service.AbnGatheringJzryService;
import com.hzlj.sqjz.jz.common.dto.jzJbxxTafxx.JzJbxxTafxxQueryResultDTO;
import com.hzlj.sqjz.jz.service.JzJbxxTafxxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.hzlj.position.config.config.IdConfig.POSITION_ID_GEN;

/**
 * 定位分析-聚众告警表-定位信息(AbnGatheringJzry)表服务接口
 *
 * @author lifh
 * @date 2023-03-28 10:20:24
 */
@Slf4j
@Service
public class AbnGatheringJzryServiceImpl implements AbnGatheringJzryService {
    @Resource
    private AbnGatheringJzryDao abnGatheringJzryDao;
    @Resource
    private JzJbxxTafxxService jzJbxxTafxxService;

    @Override
    public boolean save(String gatheringId, List<AbnGatheringCacheDTO> filterKeys) {
        Set<String> positionIds = filterKeys.stream().map(AbnGatheringCacheDTO::getPositionId).collect(Collectors.toSet());
        List<String> existsPositionIds = abnGatheringJzryDao.existsPositionIds(gatheringId, positionIds);
        List<AbnGatheringJzry> adds = filterKeys.stream()
                .filter(e -> !existsPositionIds.contains(e.getPositionId()))
                .map(e -> BeanUtil.copyProperties(e, AbnGatheringJzry.class))
                .peek(e -> e.setId(POSITION_ID_GEN.nextIdStr()))
                .peek(e -> e.setGatheringId(gatheringId))
                .collect(Collectors.toList());

        if (adds.size() == 0) {
            return false;
        }
        //判断同案犯信息
        List<Pair<String, List<JzJbxxTafxxQueryResultDTO>>> tafPairs = jzJbxxTafxxService.tafPairs(
                adds.stream().map(AbnGatheringJzry::getJzId).collect(Collectors.toList())
        );
        if (ObjectUtil.isEmpty(tafPairs)) {
            abnGatheringJzryDao.saveBatch(adds);
            return false;
        }
        //设置同案犯标记
        Map<String, List<Pair<String, List<JzJbxxTafxxQueryResultDTO>>>> tafPairsByJzId = tafPairs.stream().collect(Collectors.groupingBy(Pair::getKey));
        adds.forEach(e -> e.setTaf(tafPairsByJzId.containsKey(e.getJzId())));
        abnGatheringJzryDao.saveBatch(adds);
        return true;
    }


    /**
     * 查询列表：不分页
     *
     * @param dto 查询条件
     * @return 对象列表
     */
    @Override
    @DictConvert
    public List<AbnGatheringJzryQueryResultDTO> listAbnGatheringJzry(AbnGatheringJzryQueryParamDTO dto) {
        return this.abnGatheringJzryDao.listAbnGatheringJzry(dto);
    }


    /**
     * 查询列表：分页
     *
     * @param dto  查询条件
     * @param page 分页信息
     * @return 对象列表
     */
    @Override
    @DictConvert
    public QueryPageResultDTO<AbnGatheringJzryQueryResultDTO> pageAbnGatheringJzry(AbnGatheringJzryQueryParamDTO dto, PageParamDTO page) {
        return this.abnGatheringJzryDao.pageAbnGatheringJzry(dto, page);
    }
}
